package cn.blossoming.service.base.utils;

import org.springframework.util.Assert;

import java.util.Collections;
import java.util.List;

public class PageUtils {
    /**
     * list 转为分页list
     *
     * @param list    原list
     * @param page    第几页
     * @param perPage 每页条数
     */
    public static <T> List<T> toPage(List<T> list, int page, int perPage) {
        Assert.isTrue(page >= 0 && perPage > 0, "page >= 0 and perPage>0");
        int lastIndex = page * perPage;
        if (list.isEmpty() || list.size() < lastIndex)
            return Collections.emptyList();
        int endIndex = (page + 1) * perPage;
        if (lastIndex < list.size() && endIndex > list.size())
            return list.subList(lastIndex, list.size());
        return list.subList(lastIndex, endIndex);

    }
}
